#!/usr/bin/env python
# coding: utf-8
# vim: fdm=marker fmr={{{,}}} cms=#%s#
import sys
import os

import sqlite3
from sqlite3 import IntegrityError

class Misc:
    CLS='CLS'           # 静态变量（类变量）
    __CLS='__CLS'       # 私有静态变量（类变量）

    def __init__(self):
        self.__INST='INST'      # 私有成员以__开头，不能以__结尾

    def main(self):
        split='------------------------------------------------'
        self.__class()
        print split

        self.__sqlite()
        print split

        self.__struct()
        print split

        self.__inner()
        print split

    def __class(self):
#{{{#
        __CLASS__=str(self.__class__).split('.')[1]
        __METHOD__='__class'

        print 'module: %s' % __name__
        print 'module(class): %s' % Misc.__module__
        print 'module(instance): %s' % self.__module__
        print 'class(class): %s' % Misc.__name__
        print 'class(instance): %s' % self.__class__
        print 'class: %s' % __CLASS__
        print 'method: %s' % __METHOD__
        print 'class var: %s %s' % (self.CLS, self.__CLS)
        print 'instance private var: %s' % self.__INST
#}}}#

    def __sqlite(self):
        #{{{#
        __CLASS__=str(self.__class__).split('.')[1]
        __METHOD__='sqlite'

        try:
            db=sqlite3.connect(os.path.dirname(sys.argv[0])+'/misc.dbf')
        except Exception, e:
            print '%s.%s: connect fail: %s' % \
                (__CLASS__, __METHOD__, str(e))
            return

        cur=db.cursor()

        cur.execute('''
                create table if not exists emp
                (
                 id                 int not null,
                 update_time        datetime not null,
                 name               ,
                 primary key(id)
                )
                ''')

        paras=((1, u'张一'), (2, u'张二'), (3, u'张三'))
        for para in paras:
            try:
                cur.execute('''
                        insert into emp values(
                            ?,
                            datetime('now','localtime'),
                            ?)
                        ''', para)
            except IntegrityError, e:
                print "%s.%s: %s, pass" % (__CLASS__, __METHOD__, str(e))
                continue

        db.commit()

        para=(1,)
        cur.execute("select * from emp where id>? limit 10", para)
        for res in cur:
            print res[0], res[1], res[2]

        cur.close()

        db.close()
#}}}#

    def __struct(self):
#{{{#
        dic={ "baaaa":200, "auuuuuuuuuu":100, "dssssss":50, "csdaf":150 }

        print "natural -- ",
        for i in dic:
            print "%s:%d " % (i, dic[i]),
        print

        keys=dic.keys()
        keys.sort()
        print "sort -- ",
        for k in keys:
            print "%s:%d " % (k, dic[k]),
        print

        keys=dic.keys()
        keys.sort(None, None, True)
        print "sort reverse -- ",
        for k in keys:
            print "%s:%d " % (k, dic[k]),
        print
#}}}#

    class __Inner:
        def __init__(self):
            self.i=100

    def __inner(self):
        inner=self.__Inner()
        print inner.i

if __name__ == '__main__':
    split="================================================"

    print '%s\n' % (dir(), )
    print 'file: %s' % __file__
    print 'module: %s' % __name__
    print 'path: %s' % os.getcwd()
    print 'argv[0]: %s' % sys.argv[0]
    print 'dirname: %s' % os.path.dirname(sys.argv[0])
    print split

    misc=Misc()
    misc.main()
    print 'Misc.CLS: %s' % Misc.CLS
    print 'misc.CLS: %s' % misc.CLS
#    print 'Misc.__CLS: %s' % Misc.__CLS     # 出错
#    print misc.__sqlite()      # 出错
#    print misc.__INST      # 出错
    print split
